
package com.app.GoCuro;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.app.GoCuro.data.InputItem;
import com.app.GoCuro.util.CustomHttpClient;

public class ManualInputActivity extends Activity {
	
	String contentsToDispplay = null;
	InputItem oldData = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manualinput);
		
		Button btn_Confirm = (Button) findViewById(R.id.button_confirm);
		final EditText et_itemName = (EditText) findViewById(R.id.editText_itemname);
		final EditText et_itemQuan = (EditText) findViewById(R.id.editText_itemquantity);
		 
		
		// retrieve infor object from database action activity
		final Bundle manualInput_OldData = this.getIntent().getExtras();
		if(manualInput_OldData != null){
			oldData = manualInput_OldData.getParcelable("oldDataItem");
			et_itemName.setText(oldData.getItemName());
			et_itemQuan.setText(oldData.getQuantity());
		}
		
		btn_Confirm.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// save input and go to DatabaseActionsActivity
				String itemName_str = et_itemName.getText().toString();
				String itemQuan_str = et_itemQuan.getText().toString();
				String itemBarcode_str = "0";
				
				if(inputValidation(itemName_str,itemQuan_str)){
					
					contentsToDispplay = "item name: " + itemName_str + '\n' +
				                         "item quantity: " + itemQuan_str;
		
					Calendar c = Calendar.getInstance();
					SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String dateTime = f.format(c.getTime());
					
					//contentsToDispplay = dateTime + '\n' + contentsToDispplay;
					//Toast.makeText(getApplicationContext(), contentsToDispplay, Toast.LENGTH_SHORT).show();
					
					//create the information object
					String createDatetime;
					String modifyDatetime;
					// if modify old item
					if(oldData != null){
						createDatetime = oldData.getCreateDateTime();
						modifyDatetime = dateTime;
					}
					// if newly-created item 
					else {
						createDatetime = dateTime;
						modifyDatetime = "";
						
					}
					
					InputItem newData = new InputItem(createDatetime, modifyDatetime,  itemName_str, itemQuan_str, itemBarcode_str);
					
					// send newData to database action activity
					Intent resultIntent  = new Intent();
					Bundle manualInputData = new Bundle();
					manualInputData.putParcelable("manualInputItem_key", newData);
					resultIntent.putExtras(manualInputData);
					setResult(RESULT_OK,resultIntent);
					finish();
				
				} // end if-inputValidation
			} // end click
		});
	} 
	
	
	private boolean inputValidation(String itemName_str, String itemQuan_str) {
		
		final AlertDialog alertDialog = new AlertDialog.Builder(ManualInputActivity.this).create();
		
		if(itemName_str.isEmpty() && !itemQuan_str.isEmpty()){
			alertDialog.setTitle("Warning");
			alertDialog.setMessage("Item Name can NOT be empty.") ;
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  alertDialog.dismiss();
			      } 
			});
			alertDialog.show();  
			return false;
		}
		
		if(!itemName_str.isEmpty() && itemQuan_str.isEmpty()){
			alertDialog.setMessage("Item Quantity can NOT be empty.") ;
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  alertDialog.dismiss();
			      } 
			});
			alertDialog.show();  
			return false;
		}
		
		
		if(itemName_str.isEmpty() && itemQuan_str.isEmpty()){
			alertDialog.setMessage("Item Name and Item Quantity can NOT be empty.") ;
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  alertDialog.dismiss();
			      } 
			});
			alertDialog.show();  
			return false;
		}
		
		return true;
		
	} // end inputValidation
}
